package com.rao.crm.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.rao.crm.domain.User;
import com.rao.crm.service.UserService;
import com.rao.crm.utils.MD5Utils;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;


import java.io.IOException;
import java.util.List;

/**
 * @author Srao
 * @version 1.0
 * @className UserAction
 * @date 2019/9/18 19:31
 * @package com.rao.crm.web.action
 * @description
 */
public class UserAction extends ActionSupport implements ModelDriven<User> {

    private User user = new User();
    private UserService userService;
    private String user_password;
    private String user_id;

    public String getUser_id() {
        return user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public void setUser_password(String user_password) {
        this.user_password = user_password;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String getUser_password() {
        return user_password;
    }

    @Override
    public User getModel() {
        return user;
    }


    /*注册*/
    public String regist(){
        userService.regist(user);
        return LOGIN;
    }

    /*登陆*/
    public String login(){
        User existUser = userService.login(user);
        if (null == existUser){
            //登陆失败
            this.addActionError("用户名或密码错误！！！");
            return LOGIN;
        }else {
            //登陆成功
            //ServletActionContext.getRequest().getSession().setAttribute("existUser", existUser);
            ActionContext.getContext().getSession().put("existUser", existUser);
            return SUCCESS;
        }

    }


    public String findAllByAjax() throws IOException {
        List<User> list = userService.findAll();
        /*转JSON*/
        JSONArray jsonArray = JSONArray.fromObject(list);
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        ServletActionContext.getResponse().getWriter().println(jsonArray.toString());

        return NONE;
    }

    public String loginOut(){
        User existUser = (User) ServletActionContext.getRequest().getSession().getAttribute("existUser");
        if (existUser != null){
            ServletActionContext.getRequest().getSession().setAttribute("existUser", null);
        }
        return "loginOut";
    }

    public String changePasswordUI(){
        user_id = ServletActionContext.getRequest().getParameter("user_id");
        user = userService.findById(user.getUser_id().longValue());
        ActionContext.getContext().getValueStack().push(user);
        return "changePasswordUI";
    }


    public String changePassword(){
        user = userService.findById(user.getUser_id());
        user_password = ServletActionContext.getRequest().getParameter("user_password");
        user.setUser_password(MD5Utils.md5(user_password));
        userService.update(user);
        ServletActionContext.getRequest().getSession().setAttribute("existUser", null);
        return "changePassword";
    }
}
